/**
 * Created by Administrator on 2015/12/29.
 */
angular.module('ddcx.controller',[])
  .controller('ddcxCtrl',['$scope','$state','ddcxService','$ionicPopover','$localstorage','$ionicPopup','$cordovaBarcodeScanner',function($scope,$state,ddcxService,$ionicPopover,$localstorage,$ionicPopup,$cordovaBarcodeScanner){

    $scope.params={};
    $scope.params=ddcxService.getParams();
    $scope.$on('$ionicView.enter', function(event, data) {

      if("ddcx"==data.stateId) //订单查询
      {
        if($localstorage.get("LOG_FLAG")=="1"){
          ddcxService.insertLog("订单查询").then(function(data){
            console.log("插入日志"+angular.toJson(data));
          },function(error){
            console.log("插入日志失败，请联系管理员");
          });
        }




        //在订单查询的页面，调用查询订单类型和查询订单状态的接口
        ddcxService.queryOrderType().then(
          function(data){
            angular.copy(data, $scope.params.OrderType);
            //$scope.params.OrderType.data.unshift($scope.params.orderTypeParamsAll);
          },function(error){

          }
        );

        //在订单查询的页面，调用查询订单类型和查询订单状态的接口
        ddcxService.queryOrderStatus().then(
          function(data){
            angular.copy(data, $scope.params.OrderState);
        },function(error){

        });
        $scope.params.orderQueryParams.orderId="";
        $scope.initPageData();

      }
      else if("ddcxdetails"==data.stateId)
      {

        //console.log($scope.params.orderQueryParams);
        $scope.doRefresh();
        $scope.params.ddxk=1;
        $scope.params.ddtj=1;
        $scope.params.ddqx=1;
        $scope.params.BarSimCard={};
        //ddcxService.doRefresh($scope.params.orderQueryParams).then(function(data){
        //  angular.copy(data, $scope.params.orderData);
        //
        //  console.log(data);
        //},function(error){
        //    console.log(error);
        //});
        //$scope.params.orderQueryParams.page=1;
        //$scope.params.orderQueryParams.moreData=true;


      }else if("ddcxywkhdetails"==data.stateId){
        $scope.params.IMSI="";
        if("CBS"==$scope.params.selectOrder.SYS_CODE){
          $scope.params.distj=1;
          $scope.params.xieka=0;
          $scope.params.hqid=0;
          if(!$scope.params.myItem){
          }else{
            if($scope.params.myItem.OPENED_STATUS=="6"){
              $scope.params.distj=0;
              $scope.params.hqid=1;
            }
          }

        }
        else{
          $scope.params.distj=0;
          $scope.params.xieka=0;
          $scope.params.hqid=1;
          if($scope.params.selectOrder.CARD_TYPE=="02"){
            $scope.params.distj=1;
            $scope.params.hqid=0;
          }
        }

      }
      else if("gwddcxdetails"==data.stateId)
      {
        $scope.initGwPageData();
        $scope.doGwRefresh();
      }
    });

    $scope.showTab = 'tab-1';
    //切换tab页
    $scope.selectTab = function(_tab){
      $scope.showTab = _tab;
      if(_tab=="tab-1")
      {
        $scope.showTab = 'tab-1';
        $scope.initPageData();
      }
      if(_tab=="tab-2")
      {
        $scope.showTab = 'tab-2';
        $scope.initGwPageData();
      }
    };
    //打开开始时间日历控件
    $scope.openStartCalendar=function() {
      console.log('openCalendar');
      ddcxService.showStartDate();
    };
    //打开结束时间日历控件
    $scope.openEndCalendar=function() {
      console.log('openCalendar');
      ddcxService.showEndDate();
    };
    //固网下拉刷新
    $scope.doGwRefresh=function()
    {
      $scope.params.gworderQueryParams.page=1;
      $scope.params.gworderQueryParams.moreData=true;

      ddcxService.doGwRefresh($scope.params.gworderQueryParams).then(function(data){

        if(data.respCode=="0")
        {
          angular.copy(data.data.rows, $scope.params.gworderData);
          $scope.params.hasOrder="1";
        }
        else
        {
          $scope.params.gworderData=[]; //如果没有数据则清空数据源
          $scope.params.gworderQueryParams.moreData=false;//上拉加载不可用
          $scope.params.hasOrder="0";
        }
      },function(error){
        //$scope.params.orderQueryParams.moreData=false;
        //$scope.params.hasOrder="0";
      });
      //要让scroller知道我们已经加载完了新项目，可以隐藏加载指示。为了达到这个目的，我们要广播scroll.refreshComplete事件
      $scope.$broadcast('scroll.refreshComplete');
    }
    //固网上拉加载
    $scope.doGwLoadMore=function() {
      $scope.params.gworderQueryParams.page++;

      ddcxService.doGwRefresh( $scope.params.gworderQueryParams).then(function(data){

        if(data.respCode=="0")
        {
          //if(data.data.length==0 || $scope.params.gworderQueryParams.page>data.data.totalPages) {
          if(data.data.length==0|| $scope.params.gworderQueryParams.page>data.data.totalPages) {
            $scope.params.gworderQueryParams.moreData=false;
          } else {
            $scope.params.gworderQueryParams.moreData=true;
            $scope.params.gworderData =$scope.params.gworderData.concat(data.data.rows);
          }

          $scope.params.hasOrder="1";
        }
        else{
          $scope.params.gworderQueryParams.moreData=false;
          $scope.params.hasOrder="0";
        }
      },function(error){
        $scope.params.gworderQueryParams.moreData=false;
        $scope.params.hasOrder="0";
      }).finally(function() {
        // 停止广播ion-refresher
        $scope.$broadcast('scroll.infiniteScrollComplete');
      });
      //$scope.$broadcast('scroll.infiniteScrollComplete');
    }
    /**
     * 下拉刷新
     */
    $scope.doRefresh=function(){
      $scope.params.orderQueryParams.page=1;
      $scope.params.orderQueryParams.moreData=true;

      ddcxService.doRefresh($scope.params.orderQueryParams).then(function(data){

        if(data.respCode=="0")
        {
          angular.copy(data.data.rows, $scope.params.orderData);
          $scope.params.hasOrder="1";
        }
        else
        {
          $scope.params.orderData=[]; //如果没有数据则清空数据源
          $scope.params.orderQueryParams.moreData=false;//上拉加载不可用
          $scope.params.hasOrder="0";
        }
      },function(error){
        $scope.params.orderQueryParams.moreData=false;
        $scope.params.hasOrder="0";
      });
      //要让scroller知道我们已经加载完了新项目，可以隐藏加载指示。为了达到这个目的，我们要广播scroll.refreshComplete事件
      $scope.$broadcast('scroll.refreshComplete');
    }
    /**
     * 上拉加载
     */
    $scope.doLoadMore=function(){

      $scope.params.orderQueryParams.page++;

      ddcxService.doLoadMore( $scope.params.orderQueryParams).then(function(data){

        if(data.respCode=="0")
        {
          console.log(data.data.rows.length);
          if(data.data.rows.length==0 || $scope.params.orderQueryParams.page>data.data.totalPages) {
            $scope.params.orderQueryParams.moreData=false;
          } else {
            $scope.params.orderQueryParams.moreData=true;
            $scope.params.orderData =$scope.params.orderData.concat(data.data.rows);
          }

          $scope.params.hasOrder="1";
        }
        else{
          $scope.params.orderQueryParams.moreData=false;
          $scope.params.hasOrder="0";
        }
      },function(error){
        $scope.params.orderQueryParams.moreData=false;
        $scope.params.hasOrder="0";
      }).finally(function() {
        // 停止广播ion-refresher
        $scope.$broadcast('scroll.infiniteScrollComplete');
      });
      //$scope.$broadcast('scroll.infiniteScrollComplete');
    }

    /**
     * 初始化订单数据
     */
    $scope.initPageData=function()
    {
      $scope.params.orderQueryParams.orderSource="03";
      $scope.params.orderQueryParams.orderType="11";//默认选择缴费
      $scope.params.orderQueryParams.orderStatus="";
      $scope.params.orderQueryParams.createBeginTime="";
      $scope.params.orderQueryParams.createEndTime="";
      $scope.params.orderQueryParams.serialNumber="";
      $scope.params.orderQueryParams.rows="5";
      $scope.params.orderQueryParams.page="1";
      $scope.orderTypeName="缴费";//默认选择缴费
      $scope.orderStatusName="";

    }
    /**
     * 初始化固网订单查询数据
     */
    $scope.initGwPageData=function()
    {
      $scope.params.gworderQueryParams.orderSource="01";
      //$scope.params.gworderQueryParams.orderType="11";//默认选择缴费
      //$scope.params.gworderQueryParams.doOrderStatus="";//-- 订单状态 01 已登记  02 已经装正式受理 03 取消
      $scope.params.gworderQueryParams.cityCode="";
      $scope.params.gworderQueryParams.isRetPageData="true",
      //$scope.params.gworderQueryParams.psptId="";;
      $scope.params.gworderQueryParams.createBeginTime="";
      $scope.params.gworderQueryParams.createEndTime="";
      $scope.params.gworderQueryParams.rows="5";
      $scope.params.gworderQueryParams.page="1";
    }

    //移网开户订单的item的点击事件
    $scope.doYWKHClick=function(itemParams)
    {
      $scope.params.myItem=itemParams;
      $scope.params.BarSimCard={};
      $scope.params.selectOrder=itemParams;
      if(itemParams.CARD_TYPE=="01"){
      $scope.params.val_kplb="白卡";
        $scope.params.ddxk=1;
      }else{
        $scope.params.ddxk=0;
      $scope.params.val_kplb="成卡";
      }
     if($scope.params.selectOrder.SYS_CODE=="CBS"){
         Calcuate4G(itemParams);
     }else{
         Calcuate2G(itemParams);
     }
      //console.log(itemParams);
      if($scope.params.selectOrder.CARD_TYPE=="01"&&$scope.params.selectOrder.WRITE_STATUS!="0"){//如果订单被写过卡，则不可反复写卡
        $scope.params.ddxk=0;
        $scope.params.ddtj=0;
        $scope.params.ddqx=0;
      }
      if($scope.params.selectOrder.CARD_TYPE=="02"){//若是成卡不可写卡
        $scope.params.ddxk=0;
        $scope.params.BarSimCard.text=$scope.params.selectOrder.SIM_CARD_NO;
      }
      if(itemParams.OPENED_STATUS=="6"){
       $scope.params.hqid=1;
       $scope.params.distj=0;
        var alertPopup=$ionicPopup.alert({
          title:'友情提示',
          template:"此订单已正式提交，但未写卡",
          okText: '确定', // String (默认: 'OK')。OK按钮的文字。
          okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
        });
      }
      $state.go("ddcxywkhdetails");
    }
    //根据条形码扫描iccid
    $scope.saomiao=function(){
      $cordovaBarcodeScanner
        .scan()
        .then(function (result) {
          $scope.params.BarSimCard=angular.fromJson(result);
          //var text=$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1);
          //$scope.params.BarSimCard.text=text;
          if($scope.params.selectOrder.SYS_CODE=="CBS"){
            SimcardInfo4G();
          }else{
            SimcardInfo2G();
          }
        },
        function (error) {
        });
    };
    $scope.ReadIccid=function(){
      $scope.params.BlueSimCard={};
      var mac=$localstorage.get("mac");
   /*   $scope.params.BarSimCard.text="8986000287131651783L";
      SimcardInfo4G();*/ //测试用
      if(mac) {
        var param={"mac":mac};
        ddcxService.ReadIccid(param,function(success){
          $scope.params.BlueSimCard=angular.fromJson(success);
          if($scope.params.BlueSimCard.retCode=="0"){
            $scope.params.BarSimCard.text=$scope.params.BlueSimCard.ICCID;
              if($scope.params.selectOrder.SYS_CODE=="CBS"){
                 SimcardInfo4G();
              }else{
                SimcardInfo2G();
              }
          }else{
            var alertPopup=$ionicPopup.alert({
              title:'读取失败',
              template:"请重新尝试读取",
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
            });
          }
        },function(error){
          var alertPopup=$ionicPopup.alert({
            title:'读取失败',
            template:"请重新读取，确认已经插卡",
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
        });
/*        var result = ddcxService.ReadIccid(param);
        if(result.retCode=="0"){
          $scope.params.BarSimCard.text=result.ICCID;
          var param={
            "PREPAY_TAG":"01",//非必填，默认
            "CUST_NAME":$scope.params.selectOrder.CUST_NAME,//客户姓名
            "BUSI_TYPE":"22",//业务类型
            "PRODUCT_ID":$scope.params.selectOrder.PRODUCT_ID,//套餐编号
            "SERIAL_NUMBER":$scope.params.selectOrder.SERIAL_NUMBER,//手机号码
            "TRADE_ID":$scope.params.selectOrder.PROV_ORDER_ID,//预提交返回的省份订单编号
            "SIM_CARD":$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),//sim卡号
            "sysCode":$scope.params.selectOrder.SYS_CODE,//系统编码
            "PSPT_ID":$scope.params.selectOrder.PSPT_ID//身份证编号
          };
          //获取白卡信息
          ddcxService.SimcardInfo2G(param).then(
            function(success){
              if(success.respCode=="0"){
                $scope.params.baikaInfo=success.data;
                $scope.params.xieka=1;
              }
            },
            function(error){
            }
          );
        }else{
          var alertPopup=$ionicPopup.alert({
            title:'读取失败',
            template:angular.toJson(result),
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
        }*/

      }else{
        var alertPopup=$ionicPopup.alert({
          title:'读取失败',
          template:"请先去用户中心连接蓝牙！",
          okText: '确定', // String (默认: 'OK')。OK按钮的文字。
          okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
        });
      }
    }
    //根据条形码扫描iccid===测试
 /*   $scope.saomiao=function(){
          var param={
            "PREPAY_TAG":"01",//非必填，默认
            "CUST_NAME":"郭雷",//客户姓名
            "BUSI_TYPE":"22",//业务类型
            "PRODUCT_ID":"53354873",//套餐编号
            "SERIAL_NUMBER":"15527355454",//手机号码
            "TRADE_ID":"2716012315880855",//预提交返回的省份订单编号
            "SIM_CARD":"8986011587103672888",//sim卡号
            "sysCode":"BSS",//系统编码
            "PSPT_ID":"420323198810083131"//身份证编号
          };
          //获取白卡信息
          ddcxService.SimcardInfo2G(param).then(
            function(success){
              if(success.respCode=="0"){
                $scope.params.baikaInfo=success.data;
              }
            },
            function(error){

            }
          );
    };*/

    //正式提交
    $scope.zstijiao=function(){
      if($scope.params.selectOrder.SYS_CODE=="CBS"){
        docreateUserSubmit4G();
      }else{
        docreateUserSubmit2G();
      }
    }

    /**
     * 点击查询按钮，跳转到订单详情页面
     */
    $scope.doQuery=function()
    {
      console.log('页面跳转到:ddcxdetails.html');
      $state.go('ddcxdetails');
   /*   var mac=$localstorage.get("mac");
      var param={"mac":mac};
      ddcxService.ReadIccid(param,function(success){
        alert(success);
        alert(angular.toJson(success));
      },function(error){

      })*/
    };
    //固网订单查询
    $scope.doGwQuery=function()
    {
      console.log('页面跳转到:ddcxdetails.html');
      $state.go('gwddcxdetails');
    };



    //固网订单查询
    $ionicPopover.fromTemplateUrl('gwddzt.html', {
      scope: $scope
    }).then(function(popover) {
      $scope.popover4 = popover;
    });
    $scope.opengwddzt= function($event) {
      $scope.popover4.show($event);
    };
    $scope.closegwddzt= function(item) {
      $scope.gworderStatusName=item.typeName;
      $scope.params.gworderQueryParams.doOrderStatus=item.typeId;
      $scope.popover4.hide();
    };


    /*step4订单类型 浮动框1*/
    $ionicPopover.fromTemplateUrl('ddlx.html', {
      scope: $scope
    }).then(function(popover) {
      $scope.popover1 = popover;
    });
    $scope.openddlx= function($event) {
      $scope.popover1.show($event);
    };
    $scope.closeddlx= function(item) {
      $scope.orderTypeName=item.dataName;
      $scope.params.orderQueryParams.orderType=item.dataId;
      $scope.popover1.hide();
    };
    //开户失败详情
    $ionicPopover.fromTemplateUrl('ywkhfailreason.html', {
      scope: $scope
    }).then(function (popover) {
      $scope.popover3= popover;
    });
    $scope.openywkh= function ($event,item) {
      console.log("================*******");
      if(item.FAILURE_REASON){
        $scope.params.FAILURE_REASON = !item.FAILURE_REASON?"":item.FAILURE_REASON;
        $scope.popover3.show($event);
      }
    };
    $scope.closeywkh = function () {
      $scope.popover3.hide();
    };

    /*订单状态浮动框2*/
    $ionicPopover.fromTemplateUrl('ddzt.html', {
      scope: $scope
    }).then(function(popover) {
      $scope.popover2 = popover;
    });
    $scope.openddzt= function($event) {
      $scope.popover2.show($event);
    };
    $scope.closeddzt= function(item) {
      $scope.orderStatusName=item.dataName;
      $scope.params.orderQueryParams.orderStatus=item.dataId;
      $scope.popover2.hide();
    };

    $ionicPopover.fromTemplateUrl('openzhjffailreason.html', {
      scope: $scope
    }).then(function (popover) {
      $scope.popover = popover;
    });
    $scope.openzhjffailreason = function ($event,FAILURE_REASON) {
      if(!FAILURE_REASON){
      }else{
        $scope.params.FAILURE_REASON = FAILURE_REASON;
      }
      $scope.popover.show($event);
    };
    $scope.closetFAILURE_REASON = function () {
      $scope.popover.hide();
    };




    $scope.writeIMSI=function(){
      var mac=$localstorage.get("mac");
      if(mac){
        if($scope.params.selectOrder.SYS_CODE=="CBS"){
          writeIMSI4G();
        }else{
          writeIMSI2G();
        }
      }else{
        var alertPopup=$ionicPopup.alert({
          title:"读取失败",
          template:"蓝牙未设置，请先去用户中心设置蓝牙！",
          okText: '确定', // String (默认: 'OK')。OK按钮的文字。
          okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
        });
    }

    }
    function SimcardInfo2G(){
      var param2G={
        "PREPAY_TAG":"01",//非必填，默认
        "CUST_NAME":$scope.params.selectOrder.CUST_NAME,//客户姓名
        "BUSI_TYPE":"22",//业务类型
        "PRODUCT_ID":$scope.params.selectOrder.PRODUCT_ID,//套餐编号
        "SERIAL_NUMBER":$scope.params.selectOrder.SERIAL_NUMBER,//手机号码
        "TRADE_ID":$scope.params.selectOrder.PROV_ORDER_ID,//预提交返回的省份订单编号
        "SIM_CARD":$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),//sim卡号
       // "SIM_CARD":$scope.params.BarSimCard.text,
        "sysCode":$scope.params.selectOrder.SYS_CODE,//系统编码
        "PSPT_ID":$scope.params.selectOrder.PSPT_ID//身份证编号
      };
      //获取白卡信息
      ddcxService.SimcardInfo2G(param2G).then(
        function(success){
          if(success.respCode=="0"){
            $scope.params.baikaInfo=success.data;
            $scope.params.IMSI=$scope.params.baikaInfo.imsi;
            $scope.params.xieka=1;
          }else{
            var alertPopup=$ionicPopup.alert({
              title:"获取白卡信息失败",
              template:success.respDesc,
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
            });
          }
        },
        function(error){
        }
      );
    }
    function SimcardInfo4G(){
     var param4G={
       "ORDER_ID":$scope.params.selectOrder.ORDER_ID,
       "sysCode":$scope.params.selectOrder.SYS_CODE,
       //"SERIAL_NUMBER":$scope.params.selectOrder.SERIAL_NUMBER,
       "numId":$scope.params.selectOrder.SERIAL_NUMBER, //手机号码
       //"SIM_CARD_NO":$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),              // sim卡号
       "iccid":$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),
     }
      ddcxService.SimcardInfo4G(param4G).then(function(success){
        if(success.respCode=="0"){
          $scope.params.baikaInfo=success.data;
          $scope.params.IMSI=$scope.params.baikaInfo.imsi;
          $scope.params.xieka=1;
        }else{
          var alertPopup=$ionicPopup.alert({
            title:"获取白卡信息失败",
            template:success.respDesc,
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
        }
      },function(error){

      });
    }
   function writeIMSI2G(){
     var mac=$localstorage.get("mac");
     var param={
       "mac":mac,
       "IMSI":$scope.params.baikaInfo.imsi
     }
     var param2={
       "SERIAL_NUMBER":$scope.params.selectOrder.SERIAL_NUMBER,//手机号码
       "procid":$scope.params.baikaInfo.procid,//获取白卡信息返回的流水号
       "imsi":$scope.params.baikaInfo.imsi,//获取白卡信息返回的imsi
       "simCardNo":$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),//sim 卡号
       //"simCardNo":$scope.params.BarSimCard.text,
       "sysCode":$scope.params.selectOrder.SYS_CODE,//系统编码
     }

     ddcxService.writeIMSI(param,function(success){
       if(success=="1"){
         var alertPopup=$ionicPopup.alert({
           title:"写卡",
           template:"写卡成功！",
           okText: '确定', // String (默认: 'OK')。OK按钮的文字。
           okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
         });
         ddcxService.transmitCardOk2G(param2).then(function(success){
          $scope.params.distj=1;
         },function(error){

         });
       }else{
         var alertPopup=$ionicPopup.alert({
           title:"写卡",
           template:"写卡失败！",
           okText: '确定', // String (默认: 'OK')。OK按钮的文字。
           okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
         });
       }
     },function(error){
       var alertPopup=$ionicPopup.alert({
         title:"写卡",
         template:"写卡失败,请从新插卡再试！"+error,
         okText: '确定', // String (默认: 'OK')。OK按钮的文字。
         okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
       });
     });
   }
    function writeIMSI4G(){
      var mac=$localstorage.get("mac");
      var param={
        "mac":mac,
        "IMSI":$scope.params.baikaInfo.imsi
      }
      var param2={
        "orderId":$scope.params.selectOrder.ORDER_ID,
        "iccid": $scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),
        "imsi": $scope.params.baikaInfo.imsi,
        "procId": $scope.params.baikaInfo.procId,
        "activeId":$scope.params.baikaInfo.activeId,
        "writeResult":"0",//写卡成功失败:0成功 -1 失败,
        "reasonId":"0",//成功为0，失败为读卡器返回错误信， ,
        "errorComments":"", //s失败原因,
        "sysCode": "CBS"
      }
      console.log(angular.toJson(param));
      ddcxService.writeIMSI(param,function(success){
        if(success=="1"){
          //var alertPopup=$ionicPopup.alert({
          //  title:"写卡",
          //  template:"写卡成功！",
          //  okText: '确定', // String (默认: 'OK')。OK按钮的文字。
          //  okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          //});
          ddcxService.transmitCardOk4G(param2).then(function(success){
            if(success.respCode=="0"){
              var param3={
                "orderId":$scope.params.selectOrder.ORDER_ID,//沃受理自己的订单编号,
                "provOrderId":!$scope.params.selectOrder.PROV_ORDER_ID?"":$scope.params.selectOrder.PROV_ORDER_ID, //"省份订单编号",
                "imsi":$scope.params.baikaInfo.imsi,
                "serialNumber":$scope.params.selectOrder.SERIAL_NUMBER,
                "simId":$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),
                "cardData":$scope.params.baikaInfo.cardData,
                "procId":$scope.params.baikaInfo.procId,
                "numId":$scope.params.selectOrder.SERIAL_NUMBER,
                "capacityTypeCode":$scope.params.baikaInfo.capacityTypeCode,
                "resKindCode":$scope.params.baikaInfo.resKindCode,
                "sysCode":"CBS",
                "ordersId":$scope.params.selectOrder.ORDER_ID, //沃受理自己的订单编号
              };
              ddcxService.doCardInfoSync4G(param3).then(function(success){
                if(success.respCode=="0"){
                  var param4={
                    "ORDER_ID":$scope.params.selectOrder.ORDER_ID
                  }
                  ddcxService.finishOrder4G(param4).then(function(success){
                    var alertPopup=$ionicPopup.alert({
                      title:"写卡",
                      template:"写卡成功",
                      okText: '确定', // String (默认: 'OK')。OK按钮的文字。
                      okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
                    });
                    $state.go("maincontent");
                  },function(error){

                  });
                  //var alertPopup = $ionicPopup.alert({
                  //  title: '写卡',
                  //  template: '写卡成功', //从服务端获取更新的内容
                  //  //cancelType:'item-customer-bg-lightred',
                  //  okText: '确定',
                  //  okType:'item-customer-bg-darkred'
                  //});
                  ////alertPopup.then(function (res) {
                  ////  $state.go("maincontent");
                  ////});
                }
              },function(error){
                var alertPopup=$ionicPopup.alert({
                  title:"卡信息同步",
                  template:"卡信息同步超时",
                  okText: '确定', // String (默认: 'OK')。OK按钮的文字。
                  okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
                });
              });
            }else{
              var alertPopup=$ionicPopup.alert({
                title:"写卡失败",
                template:success.respDesc,
                okText: '确定', // String (默认: 'OK')。OK按钮的文字。
                okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
              });
            }

          },function(error){

          });
        }else{
          var alertPopup=$ionicPopup.alert({
            title:"写卡",
            template:"写卡失败！",
            okText: '确定', // String (默认: 'OK')。OK按钮的文字。
            okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
          });
        }
      },function(error){
        var alertPopup=$ionicPopup.alert({
          title:"写卡",
          template:"写卡失败,请从新插卡再试！"+error,
          okText: '确定', // String (默认: 'OK')。OK按钮的文字。
          okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
        });
      });
    }
    function docreateUserSubmit2G(){
      var param={
        "ECS_ORDER_ID":!$scope.params.selectOrder.ECS_ORDER_ID?"":$scope.params.selectOrder.ECS_ORDER_ID,//总部订单编号
        "sysCode":$scope.params.selectOrder.SYS_CODE,//系统编号
        "PROV_ORDER_ID":!$scope.params.selectOrder.PROV_ORDER_ID?"":$scope.params.selectOrder.PROV_ORDER_ID,//预提交完成返回的省份订单编号
        "ORDER_ID":  $scope.params.selectOrder.ORDER_ID
      }
      ddcxService.docreateUserSubmit2G(param).then(
        function(success){
         // console.log(success);
          if(success.respCode=="0"){
            var alertPopup=$ionicPopup.alert({
              title:"提交",
              template:"提交成功！",
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
            });
            $state.go("maincontent");
          }else{
            var alertPopup=$ionicPopup.alert({
              title:"提交",
              template:"提交失败"+success.respDesc,
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
            });
          }
          /*  var param2={
            "ORDER_ID":$scope.params.selectOrder.ORDER_ID
          }
         ddcxService.finishOrder2G(param2).then(function(success){
            var alertPopup=$ionicPopup.alert({
              title:"提交成功",
              template:"订单"+$scope.params.selectOrder.ORDER_ID+"提交成功",
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
            });
          },function(error){

          });*/
        },function(error){
        }
      );
    }
    function docreateUserSubmit4G(){
    /*  var param={
        "ECS_ORDER_ID":!$scope.params.selectOrder.ECS_ORDER_ID?"":$scope.params.selectOrder.ECS_ORDER_ID,//总部订单编号
        "sysCode":$scope.params.selectOrder.SYS_CODE,//系统编号
        "PROV_ORDER_ID":!$scope.params.selectOrder.PROV_ORDER_ID?"":$scope.params.selectOrder.PROV_ORDER_ID,//预提交完成返回的省份订单编号
      }*/
      var param={
        "provOrderId":!$scope.params.selectOrder.PROV_ORDER_ID?"":$scope.params.selectOrder.PROV_ORDER_ID,
        "ORDER_ID": $scope.params.selectOrder.ORDER_ID,          // 订单编号
        "serialNumber":$scope.params.selectOrder.SERIAL_NUMBER,       // 手机号码
        "simId":"",
        "origTotalFee":$scope.params.totalfymx,
        "sysCode":"CBS",
/*
        "ORDER_ID":  $scope.params.selectOrder.ORDER_ID,          // 订单编号
        "SERIAL_NUMBER":$scope.params.selectOrder.SERIAL_NUMBER,       // 手机号码
        "SIM_CARD_NO":$scope.params.BarSimCard.text.substring(0,$scope.params.BarSimCard.text.length-1),         // sim卡号" +
        //"SIM_CARD_NO":$scope.params.BarSimCard.text,
        "IMSI":$scope.params.baikaInfo.IMSI,               // 串号
        "RES_KIND_CODE":$scope.params.baikaInfo.RES_KIND_CODE,      // 卡类型
        "CAPACITY_TYPE_CODE":$scope.params.baikaInfo.CAPACITY_TYPE_CODE, // 卡容量
        "PROC_ID":$scope.params.baikaInfo.PROC_ID,             // 业务流水
        "CARD_DATA":$scope.params.baikaInfo.CARD_DATA,           // 白卡数据
        "sysCode":$scope.params.selectOrder.SYS_CODE,*/
      }
      ddcxService.docreateUserSubmit4G(param).then(
        function(success){
         // console.log(success);
          if(success.respCode=="0"){
            var alertPopup=$ionicPopup.alert({
              title:"提交",
              template:"提交成功！",
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
            });
            $scope.params.hqid=1;
          }else{
            var alertPopup=$ionicPopup.alert({
              title:"提交",
              template:"提交失败"+success.respDesc,
              okText: '确定', // String (默认: 'OK')。OK按钮的文字。
              okType: 'item-customer-bg-darkred', // String (默认: 'button-positive')。OK按钮的类型。
            });
          }

        },function(error){
        }
      );
    }

    function Calcuate4G(itemParams){
      if(itemParams.hasOwnProperty('SUBMIT_RESP')){
        var data=angular.fromJson(itemParams.SUBMIT_RESP);
        var data2=angular.fromJson(data.feeData).fee;
        $scope.params.fymx='';
        $scope.params.totalfymx=0;
        $scope.params.feiyong='';
        for(i=0;i<data2.length;i++){
          var fuhao=null;
          if(i==0){
            fuhao="";
          }else{
            fuhao="+";
          }
          if(data2.length!=1){
            $scope.params.totalfymx+=parseInt(data2[i].fee/1000);
          }
          $scope.params.fymx+=fuhao+data2[i].fee/1000;

          if(data2[i].feeTypeCode=="100005"){//预存款(不可清退)
            $scope.params.feiyong+=fuhao+"预存款(不可清退) ";
          }
          if(data2[i].feeTypeCode=="100001"){//普通预存款
            $scope.params.feiyong+=fuhao+"普通预存款 ";
          }
          if(data2[i].feeTypeCode=="123456"){//靓号存款
            $scope.params.feiyong+=fuhao+"靓号存款 "
          }
          if(data2[i].feeTypeCode=="1011"||data2[i].feeTypeCode=="1010"){//卡费
            $scope.params.feiyong+=fuhao+"卡费"
          }
          if(i==data2.length-1&&data2.length!=1){
            $scope.params.fymx=$scope.params.fymx+"="
          }
        }
      }else{

      }

    }

    function Calcuate2G(itemParams){
      if(itemParams.hasOwnProperty('SUBMIT_RESP')){
       var data=angular.fromJson(itemParams.SUBMIT_RESP);
        $scope.params.fymx='';
        $scope.params.totalfymx=0;
        $scope.params.feiyong='';
        for(i=0;i<data.length;i++){
          var fuhao=null;
          if(i==0){
            fuhao="";
          }else{
            fuhao="+";
          }
          if(data.length!=1){
            $scope.params.totalfymx+=parseInt(data[i].RealFee/1000);
          }else{
            $scope.params.totalfymx="";
          }
          $scope.params.fymx+=fuhao+data[i].RealFee/1000;

            $scope.params.feiyong+=fuhao+data[i].FeeDes;
          if(i==data.length-1&&data.length!=1){
            $scope.params.fymx=$scope.params.fymx+"="
          }

        }
      }else{

      }

    }
  }]);
